//ES5



//ES6
let [a, b, c, d, e = 5] = [1, 2, 3];
console.log('a:' + a);
console.log('b:' + b);
console.log('c:' + c);
//如果没匹配到，则是undefined
console.log('d:' + d);
//允许左边有默认值
console.log('e:' + e);


// let [a,b = 'hello']=['hi',undefined]; //b是hello
let [a, b = 'hello'] = ['hi', null]; //b是null
console.log('a:' + a);
console.log('b:' + b);

//对象解构
const person = {
    name: '野兽先辈',
    age: 24,
    sex: '♂'
};

//ES5
// var name = person.name;
// var age = person.age;
// var sex = person.sex;

//ES6
let { name, age, sex, occupation } = person;
console.log('name:', name);
console.log('age:', age);
console.log('sex:', sex);
console.log('occupation:', occupation);

//给左边的变量自定义命名
//冒号左边的属性名name、age是对应解构对象的属性名
//冒号右边的属性名myName、myAge是自定义的变量名

let {name :myName, age: myAge} = person;
console.log('myName:', myName);
console.log('myAge:', myAge);

// console.log('name:', name);
// console.log('age:', age);

//创建对象时也可以使用简写
let [name,occupation] = ['目力先辈','学生'];
const person_1 = {
    name,
    occupation
}
console.log('person_1: ',person_1);

//字符串解构
const [a,b,c,d] = 'hello';
console.log(a); //h
console.log(b); //b
console.log(c); //c
console.log(d); //d

console.log(typeof a);//输出的是string